﻿using Prism.Mvvm;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace MES.Desktop.Common
{
    public class DataModelBase : BindableBase, INotifyDataErrorInfo
    {
        Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();
        public bool HasErrors => _errors.Count > 0;

        public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;

        public IEnumerable GetErrors(string? propertyName)
        {
            if (!string.IsNullOrEmpty(propertyName) && _errors.ContainsKey(propertyName))
                return _errors[propertyName];
            return null;
        }

        public void RaiseErrors(string message, [CallerMemberName] string propName = "")
        {
            _errors.Add(propName, new List<string> { message });
            ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propName));
        }

        public void ClearErrors([CallerMemberName] string propName = "")
        {
            if (!string.IsNullOrEmpty(propName) && _errors.ContainsKey(propName))
                _errors.Remove(propName);
        }
    }
}
